package com.example.smartcity_poverty.ui.AllService.YouthPost

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.model.YhyoutPost.YhListAllModel
import com.example.smartcity_poverty.logic.model.YhyoutPost.YhMoreModel
import com.example.smartcity_poverty.logic.network.AppApi
import com.example.smartcity_poverty.logic.network.ServiceNET.Companion.create
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.ui.AllService.YouthPost.activity.MoreShowActivity
import com.example.smartcity_poverty.ui.AllService.YouthPost.activity.YhShowListActivity
import kotlinx.android.synthetic.main.activity_youth_post.*
import kotlinx.android.synthetic.main.re_youthpost_home_list.*
import kotlinx.android.synthetic.main.re_youthpost_home_more.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * 青年驿站
 */

class YouthPostActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_youth_post)
        title = "青年驿站"

        // 设置图片
        glide("http://124.93.196.45:10001/prod-api/profile/upload/image/2022/03/14/b86fa8e1-b556-48a5-893e-e47f0dcbd6a1.jpg", imageView6)

        // 适配器
        setMoreAdapter()

        // 列表适配器
        setListAdapter()
    }

    /**
     * 人才区域适配器
     */
    private fun setMoreAdapter() {
        val array = ArrayList<YhMoreModel.DataBean>()
        GlobalScope.launch(Dispatchers.Main){
            create<AppApi>().getYhMore(
                getSharedPreferences("token", Context.MODE_PRIVATE).getString("token", "").toString()
            ).apply {
                if (this.code == 200){
                    array.clear()
                    array.addAll(this.data!!)

                    // 填充数据
                    yhRv.apply {
                        this.layoutManager = GridLayoutManager(context, 3)
                        this.adapter = MoreAdapter(array, R.layout.re_youthpost_home_more)
                        this.isNestedScrollingEnabled = true
                    }
                }
            }
        }
    }


    /**
     * 列表适配器
     */
    private fun setListAdapter(){
        var array = ArrayList<YhListAllModel.RowsBean>()
        GlobalScope.launch(Dispatchers.Main){
            create<AppApi>().getYhUserListAll(
                getSharedPreferences("token", Context.MODE_PRIVATE).getString("token", "").toString()
            ).apply {
                if (this.code == 200){
                    array.clear()
                    array.addAll(this.rows!!)

                    // 填充数据
                    yhListRv.apply {
                        this.layoutManager = LinearLayoutManager(context)
                        this.adapter = ListAdapter(array, R.layout.re_youthpost_home_list)
                        this.isNestedScrollingEnabled = false
                    }
                }
            }
        }
    }


    // More适配器
    inner class MoreAdapter(
        val list: MutableList<YhMoreModel.DataBean>,
        val layoutId: Int
    ) : BaseRecyclerAdapter<YhMoreModel.DataBean>(list, layoutId){
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: YhMoreModel.DataBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            val v = holder.viewFind

            glide(url + d.imgUrl, v.yhImageView)

            holder.itemView.setOnClickListener {
                startActivity(Intent(v.view?.context, MoreShowActivity::class.java).putExtra("id", d.id.toString()))
            }
        }

    }


    /**
     * 列表适配器
     */
    inner class ListAdapter(
        val list: MutableList<YhListAllModel.RowsBean>,
        val layoutId: Int
    ) : BaseRecyclerAdapter<YhListAllModel.RowsBean>(list, layoutId){
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: YhListAllModel.RowsBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            val d = list[position]
            val v = holder.viewFind
            val con = v.view?.context

            glide(url + d.coverImgUrl, v.yhListImageView)
            v.yhTitle.text = d.name
            v.yhWeiOne.text = "剩余男床位: ${d.bedsCountBoy}"
            v.yhWeiTwo.text = "剩余女床位: ${d.bedsCountGirl}"
            v.yhThis.text = "驿站地点: ${d.address}"
            v.yhContent.text = "站点介绍: ${d.introduce}"

            // 点击进入查看详情
            holder.itemView.setOnClickListener {
                con?.startActivity(Intent(con, YhShowListActivity::class.java).putExtra("id", d.id.toString()))
            }
        }

    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}